<?php
namespace app\api\controller\v1;
class Goodscat extends Base{

    public function lists(){
        $page = 1;
        $limit = 8;
        if($this->post('page'))$page=$this->post('page');
        if($this->post('limit'))$limit=$this->post('limit');
        #1级分类
        $cat = $this->db->query('select * from goods_cat where parent_id = ? order by sort desc'.limit($page,$limit),[0]);
        #楼层
        foreach($cat as $k => $v){
            $rec_id = ",".($k+1).",";
            $cat[$k]['goods']  = $this->db->query("select * from goods where rec_id like '%{$rec_id}%' and status = 0 order by sort limit 12",[]);
        }
        $index = [
            'cat'=>$cat,
        ];
        suc($index);
    }

    public function getcattree(){
        #1级分类
        $cat_list = $this->db->query('select * from goods_cat where status = 0 order by sort desc,id desc',[]);
        $new_list = [];
        foreach ($cat_list as $k => $v) {
            if($v['parent_id'] != 0){
                continue;
            }
            if(isset($pid) && $v['id'] != $pid && $pid != 0){
                continue;
            }
            $child_cat = [];
            foreach ($cat_list as $ke => $va) {
                if($v["id"] == $va['parent_id']){
                    foreach ($cat_list as $key => $val) {
                        if($va["id"] == $val['parent_id']){
                            $va["child_list"][] = $val;
                        }
                    }
                    $child_cat[] = $va;
                }
            }
            $v["child_list"] = $child_cat;
            $new_list[] = $v;

        }
        $data = [
            'cat_list' => $new_list
        ];
        suc($data);
    }


}
?>
